package com.java.threaddemo1;

public class Consumer implements Runnable {
	// 属性
	private WarehouseStack whStack;
	private boolean isStopped;// 多线程终止采用外部传入参数

	// 行为
	@Override
	public void run() {

		// while死循环
		while (true) {
			synchronized (whStack) {
				// 判断栈顶指针是否为0
				if (whStack.getSize() == 0) {
					System.out.println("仓库已空，消费者进入等待阶段！");
					try {
						whStack.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (isStopped) {
						break;
					}

				}
				// 消费一个产品
				Product pro = whStack.pop();// 入栈
				System.out.println("消费产品信息：" + pro.printInfo());
				whStack.notifyAll();// 通知生产者生产
			}
			try {
				Thread.sleep(750);// 休眠1s
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	// 构造方法
	public Consumer(WarehouseStack whStack) {
		this.whStack = whStack;
	}

	public Consumer(boolean isStopped) {
		super();
		this.isStopped = isStopped;
	}
	
	
	

	public Consumer(WarehouseStack whStack, boolean isStopped) {
		super();
		this.whStack = whStack;
		this.isStopped = isStopped;
	}

	// getter，setter

	public WarehouseStack getWhStack() {
		return whStack;
	}

	public void setWhStack(WarehouseStack whStack) {
		this.whStack = whStack;
	}

	public boolean isStopped() {
		return isStopped;
	}

	public void setStopped(boolean isStopped) {
		this.isStopped = isStopped;
	}

}
